(function(win) {

	var Mummy = {
		res:{
			
		},
		txt:{
			
		},
		flag:{
			score:0,
			bulletTime:0,
			walkSpeed:300,
			jumpSpeed:700,
			downSpeed:1000,
			bulletSpeed:500
		},
		init: function() {
			this._game();
		},
		state: {
			init: {
				preload: function() {
					Mummy.res.logo=Mummy.game.load.image('bg', 'res/deep-space.jpg');
					Mummy.game.load.image('startButton', 'res/start.png');
					Mummy.game.load.spritesheet('player', 'res/metalslug_mummy37x45.png',37, 45, 18);
					Mummy.game.load.spritesheet('waters', 'res/waters.png',32, 400, 32);
				},
				create: function() {
					var __=Mummy.game, n = 7;
					
					Mummy.flag.isOnFloor=false;
					
					__.add.tileSprite(0,0,800,600,'bg');
					
					__.add.sprite(100,200,'player');
					
					Mummy.res.startButton=__.add.sprite(__.world.centerX-70,__.world.centerY,'startButton');
					
					Mummy.txt.title = __.add.text(0, 0, "Mummy Game",{ font: "bold 32px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" });
					Mummy.txt.descr = __.add.text(0, 0, "方向键控制移动、空格键射击",{ font: "bold 16px Arial", fill: "#339933", boundsAlignH: "center", boundsAlignV: "middle" });
				    Mummy.txt.title.setTextBounds(0, 100, 800, 100);
				    Mummy.txt.descr.setTextBounds(0, 140, 800, 100);
				    
				    
				    Mummy.res.ground = __.add.tileSprite(0, 24 * 16, 128 * 16, 24 * 16, 'waters');
				    Mummy.res.ground.animations.add('waves0', [0, 1, 2, 3, 2, 1]);
				    Mummy.res.ground.animations.add('waves1', [4, 5, 6, 7, 6, 5]);
				    Mummy.res.ground.animations.add('waves2', [8, 9, 10, 11, 10, 9]);
				    Mummy.res.ground.animations.add('waves3', [12, 13, 14, 15, 14, 13]);
				    Mummy.res.ground.animations.add('waves4', [16, 17, 18, 19, 18, 17]);
				    Mummy.res.ground.animations.add('waves5', [20, 21, 22, 23, 22, 21]);
				    Mummy.res.ground.animations.add('waves6', [24, 25, 26, 27, 26, 25]);
				    Mummy.res.ground.animations.add('waves7', [28, 29, 30, 31, 30, 29]);
				    Mummy.res.ground.animations.play('waves' + n, 8, true);
				    
				    Mummy.res.startButton.inputEnabled = true;
				    Mummy.res.startButton.input.priorityID = 1;
				    Mummy.res.startButton.input.useHandCursor = true;
				    Mummy.res.startButton.events.onInputDown.add(function(){
				    	__.state.start('start')
				    }, this);
				},
				update:function(){}
			},
			start:{
				preload:function(){
					Mummy.game.load.image('ground', 'res/ground.jpeg');
					Mummy.game.load.image('gameBg', 'res/background2.png');
					Mummy.game.load.image('stone', 'res/darkwing_crazy.png');
					Mummy.game.load.image('bullet', 'res/bullet.png');
					Mummy.game.load.audio('bgMusic', 'res/sd-ingame1.wav');
					Mummy.game.load.audio('shotMusic', 'res/blaster.mp3');
					Mummy.game.load.audio('deathMusic', 'res/menu_select.mp3');
				},
				create:function(){
					var __=Mummy.game;
					
					Mummy.res.gameBg=__.add.tileSprite(0, 0, 800, 600,'gameBg');
					Mummy.res.gameBg.autoScroll(-30, 0);
					//玩家攻击按钮
					Mummy.res.shutBtn = __.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
					//控制玩家跳跃按钮
					Mummy.res.cursor  = __.input.keyboard.createCursorKeys(); 
					
					//背景音乐
					Mummy.res.bgSound=__.add.audio('bgMusic');
					Mummy.res.bgSound.volume=0.1;
					//射击音乐
					Mummy.res.shotSound=__.add.audio('shotMusic');
					Mummy.res.deathSound=__.add.audio('deathMusic');
					
					//播放背景音乐
					Mummy.res.bgSound.play();
					
					Mummy.txt.score = __.add.text(0, 0, "Score:0",{ font: "bold 32px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" });
					Mummy.txt.score.setShadow(3, 3, 'rgba(0,0,0,0.5)', 2);
				    Mummy.txt.score.setTextBounds(0, 100, 800, 100);
					
					
					//添加地板图片
					Mummy.res.ground=__.add.tileSprite(0, 550, 800, 50,'ground');
					//地板自动滚动
					Mummy.res.ground.autoScroll(-100, 0);
					
					//添加玩家图片
					Mummy.res.player=__.add.sprite(100,200,'player');
					//玩家walk动画
					Mummy.res.player.animations.add('walk');
					Mummy.res.player.animations.play('walk', 20, true);
					
					//开启物理引擎
					__.physics.enable(Mummy.res.player, Phaser.Physics.ARCADE);
					__.physics.enable(Mummy.res.ground, Phaser.Physics.ARCADE);
					
					//是否启用世界碰撞
					Mummy.res.player.body.collideWorldBounds = true;
					Mummy.res.ground.body.collideWorldBounds = true;
					
					//玩家重力加速度
					Mummy.res.player.body.gravity.y = Mummy.flag.downSpeed;
					Mummy.res.ground.body.immovable = true;
					
					//创建一组障碍物
					Mummy.res.groupStone=__.add.group();
					Mummy.res.groupStone.enableBody = true;
					Mummy.res.groupStone.physicsBodyType = Phaser.Physics.ARCADE
					Mummy.res.groupStone.createMultiple(20, 'stone', 0, false);
					
					//创建一组子弹
					Mummy.res.groupBullet=__.add.group();
					Mummy.res.groupBullet.enableBody = true;
					Mummy.res.groupBullet.physicsBodyType = Phaser.Physics.ARCADE
					Mummy.res.groupBullet.createMultiple(80, 'bullet', 0, false);
					
					//障碍物出场补间动画
					__.add.tween(Mummy.res.groupStone).to({ y:-200 },1000,null,true,0,Number.MAX_VALUE,true); 
					
					//循环事件
					__.time.events.loop(2000, function(){
						var item = Mummy.res.groupStone.getFirstDead();
						//重置障碍物出现位置
						item.reset(700, __.rnd.integerInRange(200,500));
						item.outOfBoundsKill = true;
						item.checkWorldBounds = true;
						item.body.gravity.y = __.rnd.integerInRange(100,500);
						item.body.velocity.x = -__.rnd.integerInRange(100,500);
					}, this);
					
				},
				update:function(){
					var __=Mummy.game,
						_res=Mummy.res,
						_flag=Mummy.flag;
					_res.player.body.velocity.x=0;
					Mummy.txt.score.setText('Score:'+(Mummy.flag.score++));
					//玩家、地面碰撞检测
					__.physics.arcade.collide(_res.player, _res.ground,function(){
						Mummy.flag.isOnFloor=true;
					});
					
					//障碍物、地面碰撞检测
					__.physics.arcade.collide(_res.groupStone,_res.ground,function(s,g){
						s.body.velocity.y=0;
					});
					
					//障碍物、玩家碰撞检测
					__.physics.arcade.overlap(_res.player, _res.groupStone,function(p,s){
						s.kill();
						Mummy.res.bgSound.stop();
						Mummy.res.deathSound.play();
						__.state.start('end');
					});
					
					//障碍物、子弹碰撞检测
					__.physics.arcade.overlap(_res.groupBullet, _res.groupStone,function(b,s){
						s.kill();
						b.kill();
						Mummy.txt.score.setText('Score:'+(Mummy.flag.score+=100));
					});
					if(_res.cursor.right.isDown){
						_res.player.body.velocity.x = _flag.walkSpeed; 
					}else if(_res.cursor.left.isDown){
						_res.player.body.velocity.x = -_flag.walkSpeed; 
					}
					//接触地面时才开始跳跃
					if (_res.cursor.up.isDown && Mummy.flag.isOnFloor) {
						Mummy.flag.isOnFloor=false;
						_res.player.body.velocity.y = -_flag.jumpSpeed;
					}
					
					//玩家射击动作
					if(_res.shutBtn.isDown && __.time.now > Mummy.flag.bulletTime){
						var item = Mummy.res.groupBullet.getFirstDead();
						item.body.setSize(54, 17, 0, 30);
						item.scale.set(0.5);
						item.outOfBoundsKill = true;
						item.checkWorldBounds = true;
						item.reset(_res.player.x+30, _res.player.y + 8);
						item.body.velocity.x = _flag.bulletSpeed;
						Mummy.flag.bulletTime=__.time.now + 600;
						_res.shotSound.play();
					}
				}
				
			},
			
			end:{
				preload:function(){
					Mummy.game.load.spritesheet('restart', 'res/restart.png',140, 41, 2);
				},
				create:function(){
					var __=Mummy.game;
					Mummy.txt.score = __.add.text(0, 0, 'Score:'+(Mummy.flag.score),{ font: "bold 32px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" });
					Mummy.txt.end = __.add.text(0, 0, 'Game over',{ font: "bold 32px Arial", fill: "#FF0000", boundsAlignH: "center", boundsAlignV: "middle" });
					Mummy.txt.score.setShadow(3, 3, 'rgba(0,0,0,0.5)', 2);
				    Mummy.txt.score.setTextBounds(0, 100, 800, 100);
				    Mummy.txt.end.setTextBounds(0, 50, 800, 100);
				    
				    Mummy.res.restart= __.add.button(__.world.centerX-70,200,'restart',function(){
				    	Mummy.flag.score=0;
				    	__.state.start('start')
				    },this,2,1,0);
				    
				}
			}
		},
		_game: function() {
			Mummy.game=new Phaser.Game(800, 600, Phaser.AUTO, '', this.state.init);
			Mummy.game.state.add('start',Mummy.state.start);
			Mummy.game.state.add('end',Mummy.state.end);
		}
	}

	win.onload = function() {
		Mummy.init()
	}

}(window))